package com.example.stars_project_android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.ListFragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.stars_project_android.R;
import com.example.stars_project_android.adapter.GuestBookAdapter;
import com.example.stars_project_android.adapter.MessageListViewAdapter;
import com.example.stars_project_android.entity.GuestBookEntity;
import com.example.stars_project_android.entity.MessageUser;
import com.example.stars_project_android.index.RoomDetail;
import com.example.stars_project_android.index.SelfChat;
import com.google.android.material.tabs.TabLayout;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMChatRoom;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.constants.EaseConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息的fragment
 */
public class MsgPageFragment extends ListFragment {
    //定义变量的属性
    private List<MessageUser> msgList = new ArrayList<>();
    private ListView listView;
    private static final String ARG_PARAM1 = "param1";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置数据源
        msgList = setData();
        //初始化适配器
        MessageListViewAdapter messageListViewAdapter = new MessageListViewAdapter(
                getActivity(),
                R.layout.message_item,
                msgList
        );

        //绑定适配器
//        listview.setAdapter(guestBookAdapter);
        this.setListAdapter(messageListViewAdapter);
        View view = View.inflate(getContext(),R.layout.message_item,null);
    }

    @Override
    public void onResume() {
        super.onResume();
        msgList = setData();
    }

    private List<MessageUser> setData() {
        List<MessageUser> msgList = new ArrayList<>();
        MessageUser messageUser = new MessageUser();
        messageUser.setImg(R.mipmap.user_img2);
        messageUser.setUserId("BDL 4726-134-1");
        messageUser.setDate("2022-11-20");
        messageUser.setMsgData("今天看卿卿日常了吗？");
        msgList.add(messageUser);
        MessageUser messageUser1 = new MessageUser();
        messageUser1.setImg(R.mipmap.user_img1);
        messageUser1.setUserId("HDK 4326-131-4");
        messageUser1.setDate("2022-11-15");
        messageUser1.setMsgData("怎么没看到你上线呢？");
        msgList.add(messageUser1);
        MessageUser messageUser2 = new MessageUser();
        messageUser2.setImg(R.mipmap.user_img3);
        messageUser2.setUserId("LSM J2043-5610");
        messageUser2.setDate("2022-11-09");
        messageUser2.setMsgData("明天一起看剧吧！");
        msgList.add(messageUser2);
        MessageUser messageUser3 = new MessageUser();
        messageUser3.setImg(R.mipmap.user_img2);
        messageUser3.setUserId("BD+36 238");
        messageUser3.setDate("2022-11-06");
        messageUser3.setMsgData("忙着赶作业。。。");
        msgList.add(messageUser3);
        return msgList;
    }

    public static MsgPageFragment newInstance(String param1) {
        MsgPageFragment fragment = new MsgPageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_messages, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = getActivity().findViewById(android.R.id.list);
        //listView的点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Intent intent = new Intent(getActivity(),
                        SelfChat.class);
                System.out.println("这里应该跳转");
                intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID,"18329390365");
                intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EMMessage.ChatType.Chat);
                intent.putExtra(EaseConstant.EXTRA_IS_ROAM,true);
                startActivity(intent);
            }
        });
    }

}
